package com.dy.web.controller.system;

import com.alibaba.fastjson2.JSON;
import com.aliyun.sdk.service.dysmsapi20170525.models.*;
import com.dy.common.annotation.Log;
import com.dy.common.core.controller.BaseController;
import com.dy.common.core.domain.AjaxResult;
import com.dy.common.core.page.TableDataInfo;
import com.dy.common.enums.BusinessType;
import com.dy.common.utils.cms.AliyunSmsUtils;
import com.dy.system.domain.CmsAddTpl;
import com.dy.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.dy.common.utils.cms.AliyunSmsUtils.querySmsTemplateList;

@RestController
@RequestMapping("/system/tpl")
public class MobileSmsTplController extends BaseController {


    /**
     * 查询短信模板列表
     */
    @PreAuthorize("@ss.hasPermi('system:tpl:list')")
    @GetMapping("/list")
    public TableDataInfo list(Integer pageIndex,int pageSize)throws Exception{
        QuerySmsTemplateListRequest templateListRequest = QuerySmsTemplateListRequest.builder().pageIndex(pageIndex).pageSize(pageSize).build();
        QuerySmsTemplateListResponse templateList = querySmsTemplateList(templateListRequest);
        List<QuerySmsTemplateListResponseBody.SmsTemplateList> smsTemplateList = templateList.getBody().getSmsTemplateList();
        logger.info("smsTemplateList = {} ", JSON.toJSONString(smsTemplateList));
        return getDataTable(smsTemplateList);
    }




    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:tpl:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmsAddTpl cmsAddTpl)throws Exception{
        AddSmsTemplateRequest templateRequest = AddSmsTemplateRequest.builder().templateContent(cmsAddTpl.getTemplateContent())
                .templateName(cmsAddTpl.getTemplateName()).templateType(cmsAddTpl.getTemplateType()).remark(cmsAddTpl.getRemark()).build();
        AliyunSmsUtils.addSmsTemplate(templateRequest);
        return toAjax(1);
    }


    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:tpl:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String [] ids)throws Exception{
        for (int i = 0; i < ids.length ; i ++){
            DeleteSmsTemplateRequest templateRequest = DeleteSmsTemplateRequest.builder().templateCode(ids[i]).build();
            AliyunSmsUtils.deleteSmsTemplate(templateRequest);
        }
        return toAjax(1);
    }
}
